const dao = require('./base');

module.exports = {

    configList: function (condition, fields = "*", by = []) {
        let sql = `SELECT ${fields} from ex_bancor_config where 1=1`;
        let pre = [];
        if (condition.id !== undefined && condition.id !== '') {
            sql += ` and id = ?`;
            pre.push(condition.id);
        }
        sql = dao.by(sql, by);
        let limit = dao.limit(sql, condition, pre);
        sql = limit.sql;
        pre = limit.pre;

        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            throw new Error(err);
        });
    },

    configUpdate: function (condition = {}, fields = {}) {
        let sql = "update ex_bancor_config set ";
        let pre = [];
        if (fields.blockNum !== undefined && fields.blockNum !== '' && fields.blockNum !== null) {
            sql = dao.update_sql_splice(sql, "blockNum");
            pre.push(fields.blockNum);
        }
        if (fields.transferNum !== undefined && fields.transferNum !== '' && fields.transferNum !== null) {
            sql = dao.update_sql_splice(sql, "transferNum");
            pre.push(fields.transferNum);
        }

        if (condition.id !== undefined && condition.id !== '' && fields.id !== null) {
            sql += ` where id = ?`;
            pre.push(condition.id);
        } else {
            throw new Error("condition is null");
        }
        return dao.query(sql, pre).then((results, fields) => {
            return results;
        }).catch((err) => {
            console.log(sql);
            console.log(pre);
            throw new Error(err);
        });
    },


};
